﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharedGenomics.TextMining
{
    public delegate double ScoreTiling(IEnumerable<TileSection> sections);

    [CLSCompliant(true)]
    public class StringTiling : IEnumerable<TileSection>
    {
        #region Private Members

        private ScoreTiling _scoreDelegate;
        private LinkedList<TileSection> _sections;

        #endregion

        #region Constructors

        public StringTiling(ScoreTiling scoreDelegate)
        {
            this.ScoreDelegate = scoreDelegate;
            this._sections = new LinkedList<TileSection>();
        }

        public StringTiling(ScoreTiling scoreDelegate, IEnumerable<TileSection> sections)
            : this(scoreDelegate)
        {
            if (sections != null)
            {
                foreach (TileSection section in sections)
                {
                    this.Add(section);
                }
            }
        }

        #endregion

        #region Public Methods

        public void Add(TileSection tile)
        {
            if (tile == null)
                throw new ArgumentNullException("tile");

            this._sections.AddLast(tile);
        }

        public double CalculateScore()
        {
            return this.ScoreDelegate(this._sections);
        }

        #endregion

        #region Properties

        private ScoreTiling ScoreDelegate
        {
            get { return this._scoreDelegate; }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("ScoreDelegate");

                this._scoreDelegate = value;
            }
        }

        #endregion

        #region IEnumerable<TileSection> Members

        public IEnumerator<TileSection> GetEnumerator()
        {
            return this._sections.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion
    }
}